package com.mvc4g.example.mockup.client.view.views;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Event;
import com.mvc4g.example.mockup.client.Session;
import com.mvc4g.example.mockup.client.bean.EmailBean;

public class EmailListView extends AbstractView {

	private FlexTable emailTable = null;
	private List<EmailBean> emailList = null;

	@SuppressWarnings("unchecked")
	@Override
	public void init() {
		emailList = (List<EmailBean>) Session.getAttribute(EMAIL_LIST);
		int nbEmail = emailList.size();
		Label subject = null;
		for (int i = 0; i < nbEmail; i++) {
			subject = new Label(emailList.get(i).getSubject());

			final int index = i;
			subject.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					Event e = new Event(GET_EMAIL_CONTENT, index);
					controller.handleEvent(e);
				}

			});
			subject.setStyleName("emailListSubject");
			emailTable.setWidget(i + 1, 0, subject);
		}

	}

	@Override
	protected Widget createWidget() {

		emailTable = new FlexTable();
		emailTable.setHTML(0, 0, "<b>Subject</b>");

		return emailTable;
	}

}
